// 自定义hooks
import { onMounted, ref } from 'vue'
import { getApartmentListApi } from '@/api/apartment.js'
import { communityListApi } from '../api/community.js'
import { getparkingNameApi } from '../api/parking.js'

// 1.传入什么    2. 复用什么   3.返回什么

// 注意 hooks 一定要以use开头
export const useApartmentList = () => {
  // 获取所有小区名称
  const ApartmentList = ref([])
  // 定义方法
  const getApartmentList = async () => {
    const res = await getApartmentListApi()
    // console.log(res)
    // 存储数据
    ApartmentList.value = res.data
  }
  // 挂载，调用
  onMounted(() => {
    getApartmentList()
  })
  return ApartmentList
}

// todo 将时间戳转化为xxxx/xx/xx格式
export const Time = (time) => {

  const date = new Date(time);
  const year = date.getFullYear();
  const month = String(date.getMonth() + 1).padStart(2, '0');
  const day = String(date.getDate()).padStart(2, '0');
  const hours = (date.getHours());
  const minutes = (date.getMinutes());
  const seconds = (date.getSeconds());
  return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;;
}

// 获取小区 XXY
export const useCommunityList = () => {
  const communityList = ref([])
  // 全部获取小区
  const getAllCommunityList = async () => {
    const res = await communityListApi()
    // console.log(res.data);
    communityList.value = res.data
  }
  onMounted(() => {
    getAllCommunityList()
  })
  return communityList
}

//todo 将时间转化为时间戳 XXY
export const formatTime = (time) => {


  const data = time.map(element => {
    const date = new Date(element);
    return date.getTime();
  })
  return data
}

// 使用性质 XXY
export const useType = () => {
  const useStatus = [
    {
      label: '售', value: 1
    },
    {
      label: '租', value: 0
    }
  ]
  return useStatus
}

// 获取车位名称 XXY
export const parkingNameList = () => {
  const getParkingList = ref([])
  const getAllParkingList = async () => {
    const res = await getparkingNameApi()
    // console.log(res);
    getParkingList.value = res.data

  }
  onMounted(() => {
    getAllParkingList()
  })

  return getParkingList
}
